Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Create categorical variable*

    hi! new to stata/in a stats class... i have a dataset that has individual race dummy variables. Is there a way for me to merge all of the dummy variables onto one new categorical variable?

  • #2
    Hard to suggest precise code without any variable names, let alone a data example.

    This may help:

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input float(blue green magenta)
    1 0 0
    0 1 0
    0 0 1
    end
    
    gen race = cond(blue == 1, 1, cond(green == 1, 2, cond(magenta == 1, 3, .)))
    
    label def race 1 "blue" 2 "green" 3 "magenta"
    
    label val race race
    
    list
    
         +----------------------------------+
         | blue   green   magenta      race |
         |----------------------------------|
      1. |    1       0         0      blue |
      2. |    0       1         0     green |
      3. |    0       0         1   magenta |
         +----------------------------------+
    If the generate statement is hard to follow, see https://www.stata-journal.com/sjpdf....iclenum=pr0016

    or do it more slowly

    Code:
    gen race = 1 if blue == 1
    replace race = 2 if green == 1
    replace race = 3 if magenta == 1
    Also,

    Code:
    gen race = (blue == 1) + 2 * (green == 1) + 3 * (magenta == 1)
    given (e.g) blue == 1 evaluates as 1 if true and 0 if false.

    Or

    Code:
    egen race2 = group(blue green magenta)
    Whatever way you create the variable, value labels are essential for graphs and tables to be intelligible.

    Last edited by Nick Cox; 30 Apr 2019, 07:29.

    Comment


    • #3
      thank you so much! That's exactly what i was looking for

      Comment

      Working...
      X